package com.hundsun.cprs.yyt.service.system.service.impl;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.system.dao.TradeDayDao;
import com.hundsun.cprs.yyt.service.system.domain.TradeDay;
import com.hundsun.cprs.yyt.service.system.domain.TradeDayCurrent;
import com.hundsun.cprs.yyt.service.system.service.TradeDayCurrentService;
import com.hundsun.cprs.yyt.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TradeDayCurrentServiceImpl extends BaseService implements TradeDayCurrentService {

    @Autowired
    private TradeDayDao tradeDayDao;

    @Override
    public TradeDayCurrent getCurrentTradeDay(){
        TradeDayCurrent tradeDayCurrent = new TradeDayCurrent();
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = DateUtil.convertStringToDate(df.format(new Date()));
            tradeDayCurrent.setCurrentTradeDay(nowDate);
            Date nextTradeDay = nowDate;
            TradeDay nextTradeDay1= tradeDayDao.getNextTradeDay(new Date());
            if (nextTradeDay1 != null){
                nextTradeDay = nextTradeDay1.getDay();
            }
            tradeDayCurrent.setNextTradeDay(nextTradeDay);
            Date preTradeDay = nowDate;
            TradeDay preTradeDay1 = tradeDayDao.getPreTradeDay(new Date());
            if (preTradeDay1 != null){
                preTradeDay = preTradeDay1.getDay();
            }
            tradeDayCurrent.setLastTradeDay(preTradeDay);
        }catch (ParseException e){
            logger.error(e);
        }
        return tradeDayCurrent;
    }
}
